package com.npy.reggie.service.Imlp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.npy.reggie.config.CustomException;
import com.npy.reggie.doman.Dish;
import com.npy.reggie.doman.Setmeal;
import com.npy.reggie.doman.SetmealDish;
import com.npy.reggie.dto.SetmealDto;
import com.npy.reggie.mapper.SetmealMapper;
import com.npy.reggie.service.DishService;
import com.npy.reggie.service.SetmealDishService;
import com.npy.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    SetmealDishService setmealDishService;

    @Override
    @Transactional
    public void saveWithSetmealDish(SetmealDto setmealDto) {
        save(setmealDto);

        QueryWrapper<SetmealDish> queryWrapper = new QueryWrapper<>();

        if (setmealDto.getSetmealDishes()==null){
            throw new CustomException("套餐未添加菜品");
        }

        setmealDishService.saveBatch(setmealDto.getSetmealDishes().stream().map(item->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList()));

    }

    @Override
    @Transactional
    public void removeWithSteamDish(List<Long> id) {
        setmealDishService.remove(new QueryWrapper<SetmealDish>()
                .lambda().eq(id.size()!=0,SetmealDish::getSetmealId,id));

        removeByIds(id);
    }
}
